﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using System.Web.Http.Description;
using Learun.Application.Base.SystemModule;
using Learun.Application.TwoDevelopment.ZZDT_EC;
using Learun.Util;

namespace Learun.Application.Web.AppApi
{
    /// <summary>
    /// 样板文件接口
    /// </summary>
    [RoutePrefix("api/TemplateFileApi")]
    [HandlerApiLogin(FilterMode.Enforce)]
    [TokenAuthorize]
    public class TemplateFileApiController : WebApiControllerBase
    {
        private ec_template_catalogueBLL ec_template_catalogueIBLL = new ec_template_catalogueBLL();
        private ec_template_fileIBLL ec_template_fileIBLL = new ec_template_fileBLL();
        private AnnexesFileIBLL annexesFileIBLL = new AnnexesFileBLL();

        /// <summary>
        /// 获取样板目录。Tree结构，包括了样板目录和目录下的文件
        /// </summary>
        /// <param name="projectId">项目ID</param>
        /// <returns></returns>
        [HttpGet]
        [ResponseType(typeof(List<TreeModel>))]
        public IHttpActionResult GetTemplateCatalogue(string projectId)
        {
            try
            {
                var treeList = ec_template_catalogueIBLL.GetTemplateCategory(projectId);
                return Success(treeList);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }

        /// <summary>
        /// 获取样板文件信息
        /// </summary>
        /// <param name="templateFileID">样板文件ID</param>
        ///  <param name="projectId">项目ID</param>
        /// <returns></returns>
        [HttpGet]
        [ResponseType(typeof(ec_template_fileEntity))]
        public IHttpActionResult GetEntity(string templateFileID, string projectId)
        {
            try
            {
                var entity = ec_template_fileIBLL.GetEntity(templateFileID, projectId);
                var fileList = annexesFileIBLL.GetList(entity.FolderId).ToList();
                if (fileList != null && fileList.Count > 0)
                {
                    entity.FileId = fileList[0].F_Id;
                }
                return Success(entity);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }
    }
}